package com.tdkj.action.front.enterpriseZone;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Query;

import com.opensymphony.xwork2.ActionContext;
import com.tdkj.action.BaseAction;
import com.tdkj.model.decorate.BeanStyle;
import com.tdkj.model.enterprisezone.BeanEZoneDecorateBgImg;
import com.tdkj.model.enterprisezone.BeanEnterprise;
import com.tdkj.model.enterprisezone.BeanEnterpriseNews;
import com.tdkj.model.enterprisezone.BeanEnterpriseProduct;
import com.tdkj.model.enterprisezone.BeanEnterpriseProductClass;
import com.tdkj.model.enterprisezone.BeanEnterpriseZone;
import com.tdkj.model.enterprisezone.BeanEnterpriseZoneDecorate;
import com.tdkj.model.enterprisezone.BeanEnterpriseZoneFriendsLink;
import com.tdkj.model.enterprisezone.BeanEnterpriseZoneVisitor;
import com.tdkj.model.user.BeanUser;
import com.tdkj.service.decorate.IBeanStyleService;
import com.tdkj.service.enterprisezone.IBeanEnterpriseNewsService;
import com.tdkj.service.enterprisezone.IBeanEnterpriseProductClassService;
import com.tdkj.service.enterprisezone.IBeanEnterpriseProductService;
import com.tdkj.service.enterprisezone.IBeanEnterpriseService;
import com.tdkj.service.enterprisezone.IBeanEnterpriseZoneDecorateService;
import com.tdkj.service.enterprisezone.IBeanEnterpriseZoneFriendsLinkService;
import com.tdkj.service.enterprisezone.IBeanEnterpriseZoneVisitorService;
import com.tdkj.service.user.IBeanUserService;
import com.tdkj.util.PublicConfing;

public class MainAction extends BaseAction { 
	
	private BeanUser user; //当前登陆用户
	private BeanUser zoneUser;
	private BeanEnterprise enterprise;
	private BeanEnterpriseZone zone;
	private BeanEnterpriseZoneDecorate decorate;
	
	private IBeanUserService userService;
	private IBeanEnterpriseService enterpriseService;
	
	private String indexPageContent ;

	//友情链接列表
	private List<BeanEnterpriseZoneFriendsLink> friendsLinkList;
	private IBeanEnterpriseZoneFriendsLinkService enterpriseZoneFriendsLinkService;
	
	//访客列表
	private List<BeanEnterpriseZoneVisitor> visitorList;
	private IBeanEnterpriseZoneVisitorService enterpriseZoneVisitorService;
	
	//企业新闻列表
	private List<BeanEnterpriseNews> newsList;
	private BeanEnterpriseNews enews;
	private IBeanEnterpriseNewsService enterpriseNewsService;
	
	//产品分类列表jquery tree dom
	private String productClassTreeDom ;
	private IBeanEnterpriseProductClassService enterpriseProductClassService;
	
	//产品列表
	private List<BeanEnterpriseProduct> productList ; 
	private BeanEnterpriseProduct eproduct;
	private IBeanEnterpriseProductService enterpriseProductService;
	
	private IBeanEnterpriseZoneDecorateService zoneDecorateService;
	private String saveDecorateState ;
	
	private IBeanStyleService beanStyleService;
	
	private List moduleColumns = new ArrayList();
	
	@Override
	public String execute(){
		String zoneUserId = ServletActionContext.getRequest().getParameter("zoneuserid");
		user = (BeanUser) ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
		user = userService.findById(user.getId(), BeanUser.class);
		if(zoneUserId != null){
			zoneUser = userService.getUser(Long.valueOf(zoneUserId));
		}else if(user != null){
			zoneUser =	user; 
		}else{
			////跳404
		}
			enterprise = enterpriseService.findById(zoneUser.getEnterprise().getId(), BeanEnterprise.class);
			zone = zoneUser.getEnterpriseZone();
			friendsLinkList = enterpriseZoneFriendsLinkService.findByProperty("enterpriseZone", zone, BeanEnterpriseZoneFriendsLink.class);
			BeanEnterprise enterprise = zoneUser.getEnterprise();
			newsList = enterpriseNewsService.findByProperty("enterprise", enterprise ,BeanEnterpriseNews.class);

			
			productList = enterpriseProductService.findByProperty("enterprise", enterprise, BeanEnterpriseProduct.class);			
			/*查找产品分类信息*/
			BeanEnterpriseProductClass productClassCondition = new BeanEnterpriseProductClass();
			productClassCondition.setEnterprise(enterprise);
			productClassCondition.setIsAvailable(1);
			productClassCondition.setParentClass(null);
			
			List productClassList = enterpriseProductClassService.findByExample(productClassCondition);
			for(int i = 0 ; i < productClassList.size(); i ++){
				BeanEnterpriseProductClass productClass = (BeanEnterpriseProductClass) productClassList.get(i);
				if(productClass.getParentClass() != null){
					productClassList.remove(productClass);
				}
			}
			productClassTreeDom = getProducctClassUItreeDom(productClassList);
			
			//添加新访客
			if(user != null && zoneUser.getId() != user.getId() ){
				BeanEnterpriseZoneVisitor visitor = new BeanEnterpriseZoneVisitor();
				visitor.setEnterpriseZone(zone);
				visitor.setUser(user);
				//List visitors = enterpriseZoneVisitorService.findByExample(visitor);
				List visitors = enterpriseZoneVisitorService.findEnterpriseZoneVisitors(user.getId(), zone.getId());
				if(visitors.size() > 0){
					visitor = ((BeanEnterpriseZoneVisitor) visitors.get(0));	
				}
				visitor.setVisitTime(new Timestamp(System.currentTimeMillis()));
				enterpriseZoneVisitorService.saveOrUpdate(visitor, BeanEnterpriseZoneVisitor.class);
			}
			//访客列表
			zone = zoneUser.getEnterpriseZone();
			decorate = zone.getEzoneDecorate();
			visitorList = zone.getVisitor();//enterpriseZoneVisitorService.findByProperty("enterpriseZone", zone , BeanEnterpriseZoneVisitor.class);
			
			decorate = zone.getEzoneDecorate();
			String moduleLayout = decorate.getModulesLayout();
			List columnsList =  Arrays.asList(moduleLayout.split("\\|" , -1));
			for(int i = 0 ; i < columnsList.size() ; i++){
				String modulesStr = (String)columnsList.get(i);
				List moduleList =  Arrays.asList(modulesStr.split(","));
				moduleColumns.add(moduleList);
			}
		return this.SUCCESS;
	}

	public String indexPage(){
		this.execute();
		return "indexPage";
	}
	
	public String introducePage() {
		this.execute();
		return "introducePage";
	}

	public String newsPage() {
		this.execute();
		return "newsPage";
	}

	public String viewNewsContent(){
		this.execute();		
		String newId = ServletActionContext.getRequest().getParameter("newsid");
		enews = enterpriseNewsService.findById(Long.valueOf(newId), BeanEnterpriseNews.class);
		int clickNum = enews.getClickNum();
		enews.setClickNum(++clickNum);
		enterpriseNewsService.saveOrUpdate(enews, BeanEnterpriseNews.class);
		return "newsContent";
	}
	
	public String viewProductContent(){
		this.execute();
		String productId = ServletActionContext.getRequest().getParameter("productid");
		eproduct = enterpriseProductService.findById(Long.valueOf(productId), BeanEnterpriseProduct.class);	
		int clickNum = eproduct.getClickNum();
		eproduct.setClickNum(++clickNum);
		enterpriseProductService.saveOrUpdate(eproduct, BeanEnterpriseProduct.class);		
		return "productContent";
	}
	
	public String productCategoryPage() {
		this.execute();
		return "productCategoryPage";
	}

	public String photoPage() {
		this.execute();
		return "photoPage";
	}

	public String photoContent() {
		this.execute();
		return "photoContent";
	}

	public String commentsPage() {
		this.execute();
		return "commentsPage";
	}

	public String zoneDecoratePage() {
		this.execute();
		return "zoneDecoratePage";
	}

	public String videoPage() {
		this.execute();
		return "videoPage";
	}

	public String videoContentPage() {
		this.execute();
		return "videoContentPage";
	}

	public String contactWayPage(){
		this.execute();
		return "contactWayPage";
	}
	
	public String saveDecorate(){
		BeanUser user = (BeanUser) ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
		user = userService.findById(user.getId(), BeanUser.class);
		BeanEnterpriseZone zone = user.getEnterpriseZone();
		if(null != zone){
			BeanEnterpriseZoneDecorate decorate = zone.getEzoneDecorate();
			String layout = ServletActionContext.getRequest().getParameter("layout");
			String style = ServletActionContext.getRequest().getParameter("style");
			String modulesLayout = ServletActionContext.getRequest().getParameter("modulesLayout");
			String bgimgUrl =  ServletActionContext.getRequest().getParameter("bgimgurl");
			
			if(!"null".equalsIgnoreCase(layout)){
				decorate.setLayout(layout);
			}
			if(!"null".equalsIgnoreCase(modulesLayout)){
				decorate.setModulesLayout(modulesLayout);
			}
			if(!"null".equalsIgnoreCase(style)){
				decorate.setStyle(style);
			}
			if(!"null".equalsIgnoreCase(bgimgUrl)){
				BeanEZoneDecorateBgImg bgImg =  decorate.getBgImg();
				if(bgImg == null){
					bgImg = new BeanEZoneDecorateBgImg();
					decorate.setBgImg(bgImg);
				}
				bgImg.setUrl(bgimgUrl);
				
			}
			//String hql = " from " + BeanStyle.class.getName() + " as bean where bean.name='"+style+"' ";
			//Query query = this.createQuery(hql);
			
			//BeanStyle newStyle = (BeanStyle)query.uniqueResult();
			zoneDecorateService.saveOrUpdate(decorate, BeanEnterpriseZoneDecorate.class);
			//zone.setEzoneDecorate(decorate);
			saveDecorateState = "success";
		}else{
			saveDecorateState = "error";
		}
		return "saveDecorate";
	}
	
	/**
	 * 根据模块索引找出相应模块 ,主要用于装饰页的各个module.
	 */
	public String fetchModuleByIndex(){
		this.execute();
		String moduleIndex = ServletActionContext.getRequest().getParameter("moduleindex");
		if("eNotice".equals(moduleIndex)){
			return "module_eNotice";
		}else if("productClass".equals(moduleIndex)){
			return "module_productClass";
		}else if("search".equals(moduleIndex)){
			return "module_search";			
		}else if("lastVistor".equals(moduleIndex)){
			return "module_lastVistor";
		}else if("eVideo".equals(moduleIndex)){
			return "module_eVideo";
		}else if("ePhoto".equals(moduleIndex)){
			return "module_ePhoto";
		}else if("friendsLink".equals(moduleIndex)){
			return "module_friendsLink";
		}else if("eIntro".equals(moduleIndex)){	
			return "module_eIntro";
		}else if("eInfo".equals(moduleIndex)){
			return "module_eInfo";
		}else if("productIntro".equals(moduleIndex)){
			return "module_productIntro";
			
		}else if("comment".equals(moduleIndex)){
			return "module_comment";
		}else{
			//自定义	
			return "module_selfDefine";
		}
	}

	public List getModuleColumns() {
		return moduleColumns;
	}

	public void setModuleColumns(List moduleColumns) {
		this.moduleColumns = moduleColumns;
	}

	public BeanUser getZoneUser() {
		return zoneUser;
	}

	public void setZoneUser(BeanUser zoneUser) {
		this.zoneUser = zoneUser;
	}

	public BeanUser getUser() {
		return user;
	}

	public void setUser(BeanUser user) {
		this.user = user;
	}

	public BeanEnterpriseZone getZone() {
		return zone;
	}

	public void setZone(BeanEnterpriseZone zone) {
		this.zone = zone;
	}

	public BeanEnterprise getEnterprise() {
		return enterprise;
	}

	public void setEnterprise(BeanEnterprise enterprise) {
		this.enterprise = enterprise;
	}

	public String getProductClassTreeDom() {
		return productClassTreeDom;
	}

	public void setProductClassTreeDom(String productClassTreeDom) {
		this.productClassTreeDom = productClassTreeDom;
	}

	
	public String getIndexPageContent() {
		return indexPageContent;
	}

	public void setIndexPageContent(String indexPageContent) {
		this.indexPageContent = indexPageContent;
	}

		
	public BeanEnterpriseZoneDecorate getDecorate() {
		return decorate;
	}

	public void setDecorate(BeanEnterpriseZoneDecorate decorate) {
		this.decorate = decorate;
	}

	
	public String getSaveDecorateState() {
		return saveDecorateState;
	}

	public void setSaveDecorateState(String saveDecorateState) {
		this.saveDecorateState = saveDecorateState;
	}

	@Resource
	public void setEnterpriseService(IBeanEnterpriseService enterpriseService) {
		this.enterpriseService = enterpriseService;
	}

	public List<BeanEnterpriseZoneFriendsLink> getFriendsLinkList() {
		return friendsLinkList;
	}

	public void setFriendsLinkList(
			List<BeanEnterpriseZoneFriendsLink> friendsLinkList) {
		this.friendsLinkList = friendsLinkList;
	}

	@Resource
	public void setEnterpriseZoneFriendsLinkService(
			IBeanEnterpriseZoneFriendsLinkService enterpriseZoneFriendsLinkService) {
		this.enterpriseZoneFriendsLinkService = enterpriseZoneFriendsLinkService;
	}

	public List<BeanEnterpriseNews> getNewsList() {
		return newsList;
	}

	public void setNewsList(List<BeanEnterpriseNews> newsList) {
		this.newsList = newsList;
	}

	@Resource
	public void setUserService(IBeanUserService userService) {
		this.userService = userService;
	}

	@Resource
	public void setEnterpriseNewsService(
			IBeanEnterpriseNewsService enterpriseNewsService) {
		this.enterpriseNewsService = enterpriseNewsService;
	}
	
	@Resource
	public void setEnterpriseProductClassService(
			IBeanEnterpriseProductClassService enterpriseProductClassService) {
		this.enterpriseProductClassService = enterpriseProductClassService;
	}
	@Resource
	public void setBeanStyleService(IBeanStyleService beanStyleService) {
		this.beanStyleService = beanStyleService;
	}

	public List<BeanEnterpriseProduct> getProductList() {
		return productList;
	}

	public void setProductList(List<BeanEnterpriseProduct> productList) {
		this.productList = productList;
	}

	public BeanEnterpriseNews getEnews() {
		return enews;
	}

	public void setEnews(BeanEnterpriseNews enews) {
		this.enews = enews;
	}

	public BeanEnterpriseProduct getEproduct() {
		return eproduct;
	}

	public void setEproduct(BeanEnterpriseProduct eproduct) {
		this.eproduct = eproduct;
	}

	@Resource
	public void setEnterpriseProductService(
			IBeanEnterpriseProductService enterpriseProductService) {
		this.enterpriseProductService = enterpriseProductService;
	}

	
	
	public List<BeanEnterpriseZoneVisitor> getVisitorList() {
		return visitorList;
	}

	public void setVisitorList(List<BeanEnterpriseZoneVisitor> visitorList) {
		this.visitorList = visitorList;
	}
	
	@Resource
	public void setEnterpriseZoneVisitorService(
			IBeanEnterpriseZoneVisitorService enterpriseZoneVisitorService) {
		this.enterpriseZoneVisitorService = enterpriseZoneVisitorService;
	}

	@Resource
	public void setZoneDecorateService(
			IBeanEnterpriseZoneDecorateService zoneDecorateService) {
		this.zoneDecorateService = zoneDecorateService;
	}

	/**
	 * 因要求前端DIV +　CSS
	 * 在此获取信息组织easyui tree dom
	 * */
	private String getProducctClassUItreeDom(Object object){
		StringBuffer jqueryEasyUITreeDom = new StringBuffer();
		jqueryEasyUITreeDom.append("<ul id=\"jqueryEasyUITree\" class=\"easyui-tree\" animate=\"true\" dnd=\"true\">");
		jqueryEasyUITreeDom.append("<li><span><a href=\"");
		jqueryEasyUITreeDom.append("#");
		jqueryEasyUITreeDom.append("\">");
		jqueryEasyUITreeDom.append("产品分类");
		jqueryEasyUITreeDom.append("</a></span><ul>");
		jqueryEasyUITreeDom.append(buildTreeDom(object));
		jqueryEasyUITreeDom.append("</ul></li>");
		jqueryEasyUITreeDom.append("</ul>");
		return jqueryEasyUITreeDom.toString();
	}
	
	private String buildTreeDom(Object object){
		StringBuffer treeDom = new StringBuffer();
		if(object instanceof List){
			for(int i = 0 ; i < ((List<BeanEnterpriseProductClass>) object).size() ; i ++){
				treeDom.append( buildTreeDom(  ((List<BeanEnterpriseProductClass>) object).get(i) ) );
			}
			return treeDom.toString();
		}else if(object instanceof BeanEnterpriseProductClass){
			treeDom.append("<li><span><a href=\"");
			treeDom.append("#");
			treeDom.append("\">");
			treeDom.append(((BeanEnterpriseProductClass) object).getName());
			treeDom.append("</a></span>");
			List<BeanEnterpriseProductClass> subNode =  ((BeanEnterpriseProductClass) object).getSubClass();
			if(subNode != null){
				treeDom.append("<ul>");
				treeDom.append(buildTreeDom(subNode));
				treeDom.append("</ul>");
			}
			treeDom.append("</li>");
			return treeDom.toString();
		}else{
			return "";
		}
	}
}
